
#write in 2019,6,8

from flask import Blueprint,request,g, render_template,jsonify,redirect,url_for,session
from .forms import admin
from .models import admin as Admin,cmsPermission
from exts import db,mail
from flask_mail import Message
from libs.login_required import login_required
from libs import mana_cache
from libs.check_permission import check
from setting import CMS_USER_ID
import random



cms=Blueprint('cms',__name__,url_prefix='/cms')

@cms.route('/')
@login_required
@check(cmsPermission.read)
def cms_hello():
    return render_template('cms/cms_index.html')


@cms.route('/login/',methods=['GET','POST'])
def login(message=None):
    if request.method=='GET':
        form=admin()
        return render_template('cms/login.html',form=form,message=message)
    if request.method=='POST':
        form=admin(request.form)
        if form.validate():
            password=form.password.data
            email=form.email.data
            remember=form.remember.data
            user=Admin.query.filter_by(email=email).first()
            if user and user.check_password(password):
                session[CMS_USER_ID]=user.id
                if remember:
                    session.permanent=True
                return "welcome , you are the best!"
            else:
                return redirect(url_for('cms.login',message="请输入正确的邮件和密码"))
        else:
            print(form.errors)
            return redirect(url_for('cms.login',message=form.errors.popitem()[1][0]))


@cms.route('/logout/',methods=['POST'])
@login_required
@check(cmsPermission.all)
def logout():
    del session[CMS_USER_ID]
    return redirect(url_for('cms.login'))


@cms.route('/profile/',methods=['GET'])
@login_required
@check(cmsPermission.read)
def profile():
    return render_template('cms/cms_profile.html')

@cms.route('/resetpassword/',methods=['GET','POST'])
@login_required
@check(cmsPermission.read)
def resetpassword():
    if request.method=='GET':
        return render_template('cms/resetPassword.html')
    if request.method=='POST':
        user_id=session.get(CMS_USER_ID)
        user=Admin.query.get(user_id)
        form=request.form
        print('wrong')
        if user.check_password(form['oldpwd']):
            user.password=form['newpwd1']
            db.session.commit()
            return jsonify({"code":200,"message":"密码修改成功！"})
        else:
            return jsonify({"code":400,"message":"旧密码错误!"})


@cms.route('/resetmail/',methods=['GET','POST'])
@login_required
@check(cmsPermission.read)
def resetmail():
    if request.method=='GET':
        return render_template('cms/resetMail.html')

@cms.route('/getNum/',methods=['POST'])
@login_required
@check(cmsPermission.read)
def getNum():
    resipients=request.form['email']
    check=request.form['yan_zheng']
    if not check:
        check_num = str(random.randint(100000, 1000000))
        message = Message("邮件", sender='2802017651@qq.com', recipients=[resipients], body=check_num)
        mail.send(message)
        mana_cache.set("num",check_num)
        mana_cache.set("email",resipients)
    else:
        check_num=mana_cache.get("num")
        email=mana_cache.get("email")
        if resipients==email and check==check_num:
            user_id=session.get(CMS_USER_ID)
            user=g.user
            db.session.commit()
            return jsonify({"code":200,"message":"邮件已经修改成功了"})
        else:
            return jsonify({"code":400,"message":"验证码错误！"})
    return jsonify({"code":200,"message":"已经发送验证码了！"})



@cms.route('/talk')
@login_required
@check(cmsPermission.talk)
def cms_talk():
    if request.method=='GET':
        return render_template('cms/cms_talk.html')

@cms.route('/article')
@login_required
@check(cmsPermission.ariticle)
def cms_article():
    if request.method=='GET':
        return render_template('cms/cms_article.html')


@cms.route('/board')
@login_required
@check(cmsPermission.boader)
def cms_board():
    if request.method=='GET':
        return render_template('cms/cms_board.html')

@cms.route('/cms_group')
@login_required
@check(cmsPermission.m_behind)
def cms_group():
    if request.method=='GET':
        return render_template('cms/cms_group.html')

@cms.route('/cms_people')
@login_required
@check(cmsPermission.m_behind)
def cms_people():
    if request.method=='GET':
        return render_template('cms/cms_people.html')

@cms.route('/people')
@login_required
@check(cmsPermission.m_front)
def people():
    if request.method=='GET':
        return render_template('cms/people.html')

@cms.route('/group')
@login_required
@check(cmsPermission.m_front)
def group():
    if request.method=='GET':
        return render_template('cms/group.html')